package cn.anlab.anappframe.net.sub2;

import java.nio.ByteBuffer;

import org.json.JSONObject;

public class OutPacket extends Packet {
	
	public OutPacket(int cmd, int rc, byte[] body) {
		super.cmd = cmd;
		super.rc = rc;
		super.body = body;
	}
	
	public OutPacket(int cmd, int rc, String content) {
		super.cmd = cmd;
		super.rc = rc;
		super.body = content.getBytes();
	}
	
	public OutPacket(int cmd, int rc, JSONObject json) {
		super.cmd = cmd;
		super.rc = rc;
		super.body = json.toString().getBytes();
	}

	/**
	 * {cmd:2,rc:3,data:?}
	 * @return
	 */
	public byte[] getAllBytes() {
		StringBuffer sb = new StringBuffer();
		sb.append("{cmd:").append(cmd).append(",rc:").append(rc).append(",data:");
		byte[] bytes1 = sb.toString().getBytes();
		byte[] bytes2 = body;
		byte[] bytes3 = "}".getBytes();
		int length = bytes1.length + bytes2.length + bytes3.length;
		
		ByteBuffer buff = ByteBuffer.allocate(length);
		buff.put(bytes1);
		buff.put(bytes2);
		buff.put(bytes3);
		buff.flip();
		return buff.array();
	}
	
}
